package com.lagou.configx.controller;

import com.lagou.configx.conf.ConfigxDataSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * DataController
 *
 * @author wlz
 * @date 2020/5/28
 */
@RestController
public class DataController {

    @Resource
    private ConfigxDataSource configxDataSource;

    @GetMapping("/data")
    public String getData() throws SQLException {
        try (Connection connection = configxDataSource.getDataSource().getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement("select * from t_user where id = 1");
             ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                Long id = resultSet.getLong(1);
                String name = resultSet.getString(2);
                int age = resultSet.getInt(3);
                return id + "-" + name + "-" + age;
            }
            return "无数据";
        } catch (Throwable e) {
            return "查询出错";
        }
    }
}
